#$dir="/data2/bsi/RandD/Arraybased_RND/tcga_genotyping/results/BLCA/";
$dir=$ARGV[0];
chomp($dir);
#$disease="TUMOR";
$disease=$ARGV[1];
chomp($disease);
if($disease eq "NORMAL")
{
	$dir="$dir/$disease";
	$disease="normal";
}
else
{
	$dir="$dir/TUMOR";
	 $disease="tumor";
}
#die "$dir\n$disease\n";
if(-e "$dir/shapeit_jobs.tar.gz")
{
=head
	system("mkdir $dir/uncompress");
	system("tar -C $dir/uncompress/ -xzf $dir/shapeit_jobs.tar.gz");
	open(TFAM,"$dir/uncompress/23/snps_chr23.fam") or die " no file exists $dir/uncompress/23/snps_chr23.fam\n";
	$ord = 5;
	while(<TFAM>)
	{
		chomp($_);
		@array = split(" ",$_);
		$tfam_order{$array[1]}=$ord++." ".$ord++;
		#die "$tfam_order{$array[1]}\n";
	}
	$fil="$dir/new_update_gender/fwdStrandResults_all_merged_removeddup_$disease"."_build37.tfam";
	open(NEWTFAM,"$fil") or die "no file exists $fil\n";
	@neworder=(0,1,2,3,4);
	while(<NEWTFAM>)
	{
		chomp($_);
		@array = split(" ",$_);
		if(exists($tfam_order{$array[1]}))
		{
			push(@neworder,$tfam_order{$array[1]});
		}
	}
	#print "@neworder\n";
	$neworder=join(" ",@neworder);
	undef(@neworder);
	@neworder=split(" ",$neworder);
	#die "@neworder\n";
	for($i=1;$i<=23;$i++)
	{
		open(HAPS,"gunzip -c $dir/uncompress/$i/snps_chr$i.haps.gz|") or die " no file exists $dir/uncompress/$i/snps_chr$i.haps.gz\n";
		open(WRHAPS,"|gzip > $dir/uncompress/$i/newsnps_chr$i.haps.gz|") or die " cannot create file $dir/uncompress/$i/new_snps_chr$i.haps.gz\n";
		while(<HAPS>)
		{
			chomp($_);
			@array= split(" ",$_);
			#die @array." @array\n";	
			print WRHAPS $array[$neworder[0]];
			for($y=1;$y<@neworder;$y++)
			{
				if($array[$neworder[$y]] =~ m/\w/)
				{
					print WRHAPS " $array[$neworder[$y]]";
				}
				else
				{
					die "$. chr $i $array[$neworder[$y]]  not exists\n";
				}
			}
			print WRHAPS "\n";
		}
		#$count_oldtfam=`gunzip -c $dir/uncompress/$i/snps_chr$i.haps.gz|wc -l `;
		#chomp($count_oldtfam);
		#$count_oldtfam=~ s/ .+//g;
		#$count_tfam=`gunzip -c $dir/uncompress/$i/newsnps_chr$i.haps.gz|wc -l `;
		#chomp($count_tfam);
		#$count_tfam=~ s/ .+//g;	
		#$count_tfam_samp=`gunzip -c $dir/uncompress/$i/newsnps_chr$i.haps.gz|head -1|wc -w `;
		#chomp($count_tfam_samp);
		#$count_tfam_samp=~ s/ .+//g;
		#$tfam_samp_count=`wc -l $fil`;
		#chomp($tfam_samp_count);
		#$tfam_samp_count=~ s/ .+//g;
		#$tfam_samp_count=($tfam_samp_count*2)+5;
		#if($tfam_samp_count != $count_tfam_samp && $count_oldtfam!= $count_tfam)
		#{
		#	die "tfam_samp_count $tfam_samp_count\ncount_tfam_samp $count_tfam_samp\ncount_oldtfam  $count_oldtfam\ncount_tfam $count_tfam\n";
		#}
		#die "tfam_samp_count $tfam_samp_count\ncount_tfam_samp $count_tfam_samp\ncount_oldtfam  $count_oldtfam\ncount_tfam $count_tfam\n";
		system("mv $dir/uncompress/$i/newsnps_chr$i.haps.gz $dir/uncompress/$i/snps_chr$i.haps.gz");
	}
	$i=23;
	system("cp $fil $dir/uncompress/$i/snps_chr$i.fam");
=cut	
=head
	$i=23;
	open(BUFF,"$dir/uncompress/$i/snps_chr$i.fam") or die "no fie found $dir/uncompress/$i/snps_chr$i.fam\n";
	while(<BUFF>)
	{
		chomp($_);
		@array = split(" ",$_);
		$tfam{$array[1]} = $array[4];
	}
=cut
=head
	open(BUFF,"$dir/uncompress/$i/snps_chr23.sample") or die "no file found\n";
	open(WRBUFF,">$dir/uncompress/$i/new_snps_chr23.sample") or die "cannot write the sample file\n";
	$line=<BUFF>;
	print WRBUFF $line;
	$line=<BUFF>;
	print WRBUFF $line;
	while(<BUFF>)
	{
		chomp($_);
		@a = split(" ",$_);
		if(exists($tfam{$a[1]}))
		{
			$a[5] = $tfam{$a[1]};
			$_ = join(" ",@a);
			print WRBUFF $_."\n";
		}	
	}
	system("mv $dir/uncompress/$i/new_snps_chr23.sample $dir/uncompress/$i/snps_chr23.sample");
	system("tar -zcf $dir/new_update_gender/shapeit_jobs.tar.gz -C $dir/uncompress/ .");
=cut
=head
	$fil="$dir/$disease";
	$fil=$fil."_all_merged_qc";
	open(BUFF,$fil) or die "no $fil exists\n";
	$fil="$dir/new_update_gender/$disease"."_all_merged_qc";
	open(WRBUFF,">$fil") or die " not able to write file $fil \n";
	$line=<BUFF>;
	print WRBUFF $line;
	while(<BUFF>)
	{
		@a=split("\t",$_);
		if(exists($tfam{$a[0]}))
		{
			print WRBUFF $_;
		}	
	}
=cut
system("tar -zcf $dir/new_update_gender/shapeit_jobs.tar.gz -C $dir/uncompress/ .");
}
else
{
	print "no shapeit file exists\n";
}
